package UPPM;

import java.util.NoSuchElementException;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

//import com.gargoylesoftware.htmlunit.html.Util;

public class Utils {

	public static final String USER_NAME = "cristina.turlea@endava.com";
	public static final String PASSWORD = "a";
	
	public static void waitForElement(final WebDriver driver, final By by) {
		(new WebDriverWait(driver, 50))
				.until(new ExpectedCondition<WebElement>() {
					public WebElement apply(WebDriver d) {
						return driver.findElement(by);
					}
				});

	}

public static boolean isElementPresent(final WebDriver driver, final By by) {
		  
		  try {
		   driver.findElement(by);
		   
		      return true;
		  } catch (NoSuchElementException e) {
		   return false;
		  }
	}

//public static void acceptCookies (final WebDriver driver) {
	
//	Cookie cookie = new Cookie("cookiesallowed", "true");
//	driver.manage().addCookie(cookie);
//	WebElement cookiesCheckBox = driver.findElement(By.cssSelector("input#acceptCookie"));
//	cookiesCheckBox.click();

//}

	
public static void Login (final WebDriver driver) throws InterruptedException {

	//acceptCookies(driver);
	Thread.sleep(2000);
	WebElement loginLink = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[2]/div/div/ul/li[5]/a"));
	loginLink.click();
	Thread.sleep(2000);
	Utils.waitForElement(driver, By.id("login_overlay"));
	
	WebElement user = driver.findElement(By.id("loginUser")); 
	Thread.sleep(2000);
	user.click();
	user.sendKeys("example@example.com");
	Thread.sleep(2000);
	WebElement password = driver.findElement(By.id("loginPwd"));
	password.sendKeys("UMPGPassword");
	WebElement loginBtn = driver.findElement(By.id("loginButton"));
	loginBtn.click();

	/*waitForElement(driver, By.id("companies"));
	WebElement companies_btn = driver.findElement(By.id("selectCompanyButton"));
	
//	companies_btn.click();
	Thread.sleep(1000);
	waitForElement(driver, By.id("download_manager"));*/
	

}

public static void AdminLogin (final WebDriver driver){

	driver.get("http://testadmin.unippmtest.co.uk/");
//	driver.get("http://admin.unippmtest.co.uk");
	waitForElement(driver, By.id("LoginControl_LoginButton"));
	
	WebElement user = driver.findElement(By.id("LoginControl_UserName"));
	user.sendKeys("lenuta.hotea@endava.com");
//	user.sendKeys("adewale.adekoya@umusic.com");
	WebElement pass = driver.findElement(By.id("LoginControl_Password"));
	pass.sendKeys("a");
	//pass.sendKeys("password");
	WebElement login = driver.findElement(By.id("LoginControl_LoginButton"));
	login.click();

	waitForElement(driver, By.id("ctl00_SelectSiteDropDown"));
}

public static void CueSheetCheckBtns (final WebDriver driver){

	WebElement back_btn = driver.findElement(By.id("back_btn"));
	Assert.assertEquals(true, back_btn.isEnabled());
	WebElement save_btn = driver.findElement(By.id("save_btn"));
	Assert.assertEquals(true, save_btn.isEnabled());
	WebElement cancel_btn = driver.findElement(By.id("cancel_btn"));
	Assert.assertEquals(true, cancel_btn.isEnabled());
	WebElement next_btn = driver.findElement(By.id("next_btn"));
	next_btn.click();

}

public static void CompanyAdminUserLogin (final WebDriver driver){

	WebElement loginLink = driver.findElement(By.linkText("Login"));
	loginLink.click();
	
	Utils.waitForElement(driver, By.id("login_overlay"));
	
	WebElement user = driver.findElement(By.id("loginUser"));
	user.sendKeys("adewale.adekoya@umusic.com");
	
	WebElement password = driver.findElement(By.id("loginPwd"));
	password.sendKeys("password");
	
	WebElement loginBtn = driver.findElement(By.id("loginButton"));
	loginBtn.click();
	
	waitForElement(driver, By.id("companies"));
	WebElement companies_btn = driver.findElement(By.id("selectCompanyButton"));
	companies_btn.click();
	
	waitForElement(driver, By.id("download_manager"));

}
}
